<?php
class Leader extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
        $this->load->helper('url_helper');
		$this->load->library('session');
		$this->load->library('encryption');
		$this->load->model("user_model");
		$this->load->model("leader_model");
	}

	public function indexView()
	{
		$data = array();
		if(!isset($_SESSION['user_state']))
		{
			$this->load->view('main',$data);
		}
		else
		{
			if($this->user_model->isLeader() == "yes")
			{
				$data['content'] = $this->leader_model->getLeaveApplyAll(0);
				$this->load->view("leader/index",$data);
			}
			else
			{
				echo "<script>location.href='/'</script>";
			}
			
		}
	}

	public function workerView()
	{
		$id = $this->uri->segment(2);
		$data = array();
		if(!isset($_SESSION['user_state']))
		{
			$this->load->view('main',$data);
		}
		else
		{
			if($this->user_model->isLeader() == "yes")
			{
				$data['content'] = $this->leader_model->getLeaveApplyById(0,$id);
				$data['id'] = $id;
				$this->load->view("leader/worker",$data);
			}
			else
			{
				echo "<script>location.href='/'</script>";
			}
			
		}
	}

	public function getAllLinkedWorkersAction()
	{
		$data['content'] = $this->leader_model->getSubWorkerState($_SESSION['user_id']);
		$data['state'] = !empty($data['content']);
		echo json_encode($data);
	}

	public function passLeaveAction()
	{
		$data = $this->input->post();
		if(!isset($_SESSION['user_state']))
		{
			echo json_encode(array("code"=>"101","text"=>"Please Log in"));
		}
		else
		{
			if($this->user_model->isLeader() == "yes")
			{
				echo json_encode($this->leader_model->passLeaveState($data));
			}
			else
			{
				echo json_encode(array("code"=>"101","text"=>"No Permission"));
			}
			
		}
	}
}